import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {
    static Logger logger = LoggerFactory.getLogger(Util.class);

    public static void main(String[] args) throws IOException {
        /*
         * this is for test
         * */

        File[] listFiles = new File("D:\\所有程序\\程存淦\\Java\\银行\\江苏监管局\\非表格").listFiles();

        //File file_one = new File("D:\\所有程序\\程存淦\\Java\\银行\\江苏监管局\\非表格\\1010_中国保监会江苏监管局行政处罚决定书﹝2018﹞54号_2018-07-03.html");


        for (File file : listFiles) {
            Document html = Jsoup.parse(file, "GBK", "http://example.com/");

            String str = GetWenHao(html, file);
            logger.info(str);
        }
    }

    static Matcher GetMatcher(String regex, String input) {
        //简化正则
        return Pattern.compile(regex).matcher(input);
    }

    static String InitString(String input) {
        return input.replace("/\\s+/g", "").trim();
    }

    static String GetWenHao(Document html, File file) {
        String html_text = html.text();

        Pattern pattern = Pattern.compile(".*?保监.*?号{1}");
        Matcher matcher = pattern.matcher(html_text);
        if (matcher.find()) {
            String str = matcher.group();
            int start = 0;
            if (str.contains("处罚决定书")) {
                start = str.indexOf("处罚决定书") + 5;
            }
            int index = str.indexOf("号") + 1;
            start = start > index ? 0 : start;
            return str.substring(start, index);
        }


        pattern = Pattern.compile("（.*?保监.*?号{1}");
        matcher = pattern.matcher(file.getName());
        if (matcher.find()) {
            String str = matcher.group();
            int index = str.indexOf("号") + 1;
            return str.substring(1, index);
        }

        pattern = Pattern.compile("\\(.*?监.*?号{1}");
        matcher = pattern.matcher(file.getName());
        if (matcher.find()) {
            String str = matcher.group();
            int index = str.indexOf("号") + 1;
            return str.substring(0, index);
        }

        return "";
    }

    //    行政处罚决定
    static String GetJueding(List<String> list, Document html) {

        StringBuffer sb = new StringBuffer();

        String html_text = html.text();

        Pattern pattern = Pattern.compile("(警告).*?罚款");
        Matcher matcher = pattern.matcher(html_text);
        while (matcher.find()) {
            sb.append(matcher.group() + "; ");
        }
        if (sb.length() != 0)
            return sb.toString();

        pattern = Pattern.compile("我分?局责令.*?罚款");
        matcher = pattern.matcher(html_text);
        while (matcher.find()) {
            String str = matcher.group();
            str = str.substring(str.indexOf("局") + 1) + ";";
            sb.append(str);
        }
        if (sb.length() != 0)
            return sb.toString();

        pattern = Pattern.compile("我分?局作出.*?如下(行政)?处罚.*?罚款");
        matcher = pattern.matcher(html_text);
        while (matcher.find()) {
            String str = matcher.group();
            str = str.substring(str.indexOf("对") + 1) + ";";
            sb.append(str);
        }
        if (sb.length() != 0)
            return sb.toString();

        pattern = Pattern.compile("作出.*行政处罚");
        matcher = pattern.matcher(html_text);
        while (matcher.find()) {
            String str = matcher.group();
            str = str.substring(str.indexOf("出") + 1) + ";";
            sb.append(str);
        }
        if (sb.length() != 0)
            return sb.toString();

        pattern = Pattern.compile("我分?局决定.*的(行政)?处罚");
        matcher = pattern.matcher(html_text);
        while (matcher.find()) {
            String str = matcher.group();
            str = str.substring(str.indexOf("定"), str.length() - 2);
            sb.append(str);
        }
        return sb.toString();
    }


    //作出处罚决定的机关名称
    static String GetName(String fName) {
        //       System.out.println(fName);
        String[] s_all = fName.split("_");
        if (s_all.length > 0 && s_all[1].contains("行政处罚决定书")) {
//            System.out.println(s_all[1].substring(0,s_all[1].indexOf("行政处罚决定书")));
            return s_all[1].substring(0, s_all[1].indexOf("行政处罚决定书"));
        }
        return null;
    }

    //主要违法违规事实（案由）
    static String GetAnyou(List<String> list, Document doc) {
        StringBuffer sb = new StringBuffer();
        boolean add = false;
        for (String s : list) {
            if (s.startsWith("综上") || s.contains("上述行为")) {
                break;
            }
            if (add) {
                sb.append(s);
            }
            if (s.startsWith("经查"))
                add = true;

        }

        if (sb.toString().startsWith("上述行为") || sb.toString().startsWith("该公司上述行为") || sb.toString().startsWith("你公司上述行为"))
            sb.delete(0, sb.length());

        if (sb.length() == 0) {
            add = false;
            for (String s : list) {
                if (s.contains("上述") && add) {
                    sb.append(s);
                    break;
                }
                if (add) {
                    sb.append(s);
                }
                if (s.endsWith("审理终结。"))
                    add = true;
            }
        }


        if (sb.length() == 0) {
            for (String s : list) {
                if (s.startsWith("经查")) {
                    sb.append(s);
                }
            }
        }

        if (sb.length() == 0) {
            String html_text = doc.text();
            String pattern = "(经查.*[\\s\\S]*综上)|(经查.*[\\s\\S]*上述)|(题：.*[\\s\\S]*上述)";
            Pattern re = Pattern.compile(pattern);
            Matcher matcher = re.matcher(html_text);
            while (matcher.find()) {
                String str = matcher.group();
                sb.append(str.substring(2, str.length() - 2));
            }
        }

        if (sb.length() == 0) {
            String html_text = doc.text();
            String pattern = "为：.*[\\s\\S]*上述行为";
            Pattern re = Pattern.compile(pattern);
            Matcher matcher = re.matcher(html_text);
            while (matcher.find()) {
                String str = matcher.group();
                sb.append(str.substring(2, str.length() - 4));
            }
        }

        return sb.toString();
    }


    static List<String> GetFileStr(File input) throws IOException {
        Document doc = Jsoup.parse(input, "GBK", "http://example.com/");
        List<String> list = new ArrayList<>();

        Elements children = doc.body().children();
        while (children.size() <= 1) {
            children = children.first().children();
        }
        for (Element child : children) {
            String str = child.text().trim().replace(" ", "").replace(" ", "");
            if (str.length() > 0 && !str.equals(" "))
                list.add(str);
        }

        return list;
    }


    //    行政处罚依据
    static String GetBasis(List<String> list) throws IOException {
        List<String> basis_info = new ArrayList<>();

        String pattern = "违反了.*?条?项?(的)?规定";
        Pattern re = Pattern.compile(pattern);

        for (String i : list) {
            Matcher matcher = re.matcher(i);
            while (matcher.find()) {
                String s = matcher.group();
                basis_info.add(s.substring(3, s.length() - 3)); //去除开头的  违反了   结尾的   的规定
            }
        }

        // [《保险公司管]
        if (basis_info.toString().equals("[《保险公司管]")) {
            pattern = "违反了.*?条?项?的规定";
            re = Pattern.compile(pattern);
            for (String i : list) {
                Matcher matcher = re.matcher(i);
                while (matcher.find()) {
                    String s = matcher.group();
                    basis_info.add(s.substring(3, s.length() - 3)); //去除开头的  违反了   结尾的   的规定
                }
            }
        }

        //[《保险专业代理机构监]
        if (basis_info.toString().equals("[《保险专业代理机构监]")) {
            pattern = "(违反了.*?的规定)|(违反了.*?条)";
            re = Pattern.compile(pattern);
            for (String i : list) {
                Matcher matcher = re.matcher(i);
                while (matcher.find()) {
                    String s = matcher.group();
                    basis_info.add(s.substring(3, s.length() - 3)); //去除开头的  违反了   结尾的   的规定
                }
            }
        }

        //[]
        if (basis_info.toString().equals("[]")) {
            pattern = "(构成了.*?条?项?的违规)|(依据.*?条?项?的规定)|(根据.*?条?项?的规定)";
            re = Pattern.compile(pattern);
            for (String i : list) {
                Matcher matcher = re.matcher(i);
                while (matcher.find()) {
                    String s = matcher.group();
                    basis_info.add(s.substring(3, s.length() - 3)); //去除开头的  违反了   结尾的   的规定
                }
            }
        }

        //[]
        if (basis_info.toString().equals("[]")) {
            pattern = "(违反.*条)|(根据.*条)";
            re = Pattern.compile(pattern);
            for (String i : list) {
                Matcher matcher = re.matcher(i);
                while (matcher.find()) {
                    String s = matcher.group();
                    basis_info.add(s.substring(2)); //去除开头的  违反了   结尾的   的规定
                }
            }
        }


        return basis_info.toString();
    }

    //处罚日期
    static String GetDate(Document html) {
        String html_text = html.text();

        Pattern pattern = Pattern.compile(".{4}年.{1,3}月.{1,5}日");
        Matcher matcher = pattern.matcher(html_text);

        List<String> list = new ArrayList<>();
        if (matcher.find()) {
            list.add(matcher.group());
        }

        int n = list.size();
        if (n > 0)
            return list.get(n - 1);

        return "";
    }

    //处罚对象
    static String GetChuFaDuiXiang(List<String> strings_list) {
        for (String str : strings_list) {


            Pattern pattern = Pattern.compile("当事人姓名：?:?.*");

            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                return name.substring(6);
            }


            pattern = Pattern.compile("(受处罚单位：?:?.*)|(受处罚机构：.*)|(被处罚单位：.*)|(主要负责人：.*)");

            matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                if (name.contains("地址")) {
                    int index = name.indexOf("地址");
                    name = name.substring(0, index);
                }
                return name.substring(6);
            }

            pattern = Pattern.compile("(受处罚人：?:?.*)|(单位名称：.*)|(个人姓名：.*)|(受罚单位：.*)|(被处罚人：.*)");

            matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                if (name.contains("经查")) {
                    int index = name.indexOf("经查");
                    name = name.substring(0, index);
                } else if (name.contains("地址")) {
                    int index = name.indexOf("地址");
                    name = name.substring(0, index);
                }

                return name.substring(5);
            }


            pattern = Pattern.compile("(被处罚单位名称:.*)|(被处罚单位名称：.*)|(被处罚个人姓名：.*)");

            matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                return name.substring(8);
            }


            pattern = Pattern.compile("被处罚人姓名：.*");

            matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                return name.substring(7);
            }


            pattern = Pattern.compile("(当事人：?:?.*)|(受罚人：.*)");

            matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                if (name.length() > 30 || name.contains("具的说明等证据证明"))
                    continue;
                return name.substring(4);
            }


        }

        return "";
    }
}
